/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   BasicQualityServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz    </p>
 * 
 * <p>最后修改 : $: 2017年7月31日-下午5:26:32 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.informatization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.jsbz.BasicInfoMentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.BasicInfoMent;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.informatization.BasicQualityService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * BasicQualityServiceImpl
 * 
 * 2017年7月31日 下午5:26:32
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class BasicQualityServiceImpl implements BasicQualityService{
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public BasicInfoMentDao BasicInfoMentDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<BasicInfoMent> getBasicInfoMentList(QueryFilter queryFilter){
		return BasicInfoMentDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getBasicInfoMent(String stratTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<BasicInfoMent> basicInfoMentList = new ArrayList<BasicInfoMent>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = checkTable.getYearFull() == null? Double.parseDouble((String)jsonObject.get("yearFull")) : checkTable.getYearFull();
		double yearPart = checkTable.getYearPart() == null? Double.parseDouble((String)jsonObject.get("yearPart")) : checkTable.getYearPart();
		String fileNameId = (String)jsonObject.get("fileNameId");
		//取到所有文件
		List<Attachment> list = systemManageService.getCheckAttachmentsByPartId(stratTime, endTime, tag, fileNameId);
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("reportTime", stratTime, MatchType.GE);
		queryFilter.addFilter("reportTime", endTime, MatchType.LE);
		//返回时间
		int years[] = new int[Integer.parseInt(endTime) - Integer.parseInt(stratTime)+1];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(stratTime)+i;
		}
		//查询得分情况
		double total = 0.0;				//年度累计得分
		double percentage = 0.0;		//完善度
		double temp = 0.0;				//总分
		double sum = MathsUtils.mulDouble(yearFull, years.length);
		
		//1表示表扬，2未批评未表扬，3批评
		if (StringUtils.isEmpty(fileNameId)) {
			basicInfoMentList = this.getBasicInfoMentList(queryFilter);
		} else {
			queryFilter.addFilter("id", fileNameId, MatchType.EQ);
			basicInfoMentList = this.getBasicInfoMentList(queryFilter);
			for (BasicInfoMent basicInfoMent : basicInfoMentList) {
				if (basicInfoMent.getCheckResult().equals("1")) {
					total += yearFull;
				} else if (basicInfoMent.getCheckResult().equals("2")) {
					total += yearPart;
				} {
					total += 0.00;
				}
				temp += total;
				basicInfoMent.setYearMark(total);
			}
			values.add(fileNameId);
			//判断完善度
			percentage = MathsUtils.divDouble(temp, sum, 2);
			percentage  = MathsUtils.formatDouble(percentage*100, 2);
			map.put("percent", percentage);
			map.put("years", years);
		}
						
		
		map.put("att", list);
		map.put("checkTable", checkTable);
		map.put("sum", temp);
		map.put("basicInfoMent", basicInfoMentList);
		checkTableService.updateCheckTable(checkTable, percentage, temp, sum, values);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
